<?php

class DefaultController extends FrontBaseController
{
	public static $containKeysOnly = array(); //modify this if needed
	//public $layout = '//layouts/login';
	public $title = '用户注册-代码客栈';
	public $pageTopTitle = '';
	//public $cssFileName = 'regcss.css';
	
	/**
	 * (non-PHPdoc)
	 * @see CController::init()
	 */
	public function init() {
		parent::init();
		//yii::app()->theme='2013';
	}
	public function filters() {
	    return array(
				'RequestI+ regPost',
				'Post+ regPost',
				'Get + UsernameValidate,EmailValidate',
		);
	
		
	} 
	
	public function filterPost($filterChain) {
		if (!Yii::app()->request->getIsPostRequest()) {
			throw new CHttpException(403, '非法操作！');
		}
		//$patten = '/^*admin*$/';
	
		$parmas = array('User' => $_POST['User']);
		$account = new AccountDao($parmas);
		if (!isset($_POST['User']['userpwd']) || !$_POST['User']['userpwd'] || !isset($_POST['User']['username']) || !$_POST['User']['username']) {
			echo "<script>alert('用户名，密码不能为空');</script>";
			echo "<script>parent.location.href='?r=user/register/person'</script>";
			return;  
		}
		$_POST['User']['userpwd'] = XxteaD::encrypt(UtilD::deSlashes($_POST['User']['userpwd']), Yii::app()->params['frontpassportkey']);
		$_POST['User']['username'] = strtolower($_POST['User']['username']);
		$response1 = $account->isUsernameCanUse();
		$response1 = CJSON::decode($response1);
		$response2 = $account->isEmailCanUse();
		$response2 = CJSON::decode($response2);
		/* if($response1['key']===0 || $response2['key']===0){
		 $this->display($account->User,'user','personRegisterWidget');
		return;
		} */
	
		if ($response1['key'] === 0 || $response2['key'] === 0) {
			$error = "";
			if ($response1['key'] === 0) {
				$error = $response1['keyMain'] . "\\n";
			}
			if ($response2['key'] === 0) {
				$error = $error . $response2['keyMain'];
			}
			//  $this->display($account->User,'user','personRegisterWidget');
			echo "<script>alert('" . $error . "');</script>";
			echo "<script>parent.location.href='?r=user/register/person'</script>";
			return;
		}
		$filterChain->run();
	}
	
	public function actionIndex()
	{
		$this->render('index'); 
	}
	
	public function actionLogin(){
		$this->render('login');
	}
	
	/**
	 * 登陆提交
	 */
	public function actionloginPost(){
		$uname = UtilD::deSlashes(Yii::app()->request->getParam('username'));
		$upwd  = UtilD::deSlashes(Yii::app()->request->getParam('pwd'));
	}
	
	/**
	 * 注册提交
	 */
	public function actionregPost(){
		$user = Yii::app()->request->getParam('User');
		$parmas = array('User' => $user);
		$account = new AccountDao($parmas);
		$response = $account->register();
		$account->login();   //注册后自动登陆
	}
}